﻿using ConsoleAppTemp.TemplatePdf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace ConsoleAppTemp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var pdfClass = Assembly.Load("ConsoleAppTemp").GetTypes()
              .Where(c => c.IsClass && c.Name == $"ZCOrderPdfDataInfo")
              .FirstOrDefault();
            var paramses = new object[] {"123131311"};
            BaseTemplate pdf = Activator.CreateInstance(pdfClass, paramses) as BaseTemplate;

            pdf.GetCustomerHeader();

            var s = System.Text.Encoding.GetEncodings();
            string filepath = GetFileJson($@"F:\\天猫双12商品图片数据\\天猫双12商品图片数据1.json");

            SqlSugarDBContext db = new SqlSugarDBContext();
            Random randPrice = new Random();
            var catCloth = db.Db.Ado.SqlQuery<List<int>>("SELECT id FROM `tcommoncategory` WHERE ParentId=9");
            var catChaoShi = db.Db.Ado.SqlQuery<List<int>>("SELECT id FROM `tcommoncategory` WHERE ParentId=21");
            var catBao = db.Db.Ado.SqlQuery<List<int>>("SELECT id FROM `tcommoncategory` WHERE ParentId=22");
            //var cloth =

            var list = JsonConvert.DeserializeObject<List<Goods>>(filepath);
            string sql = string.Empty;
            for (int i = 600; i < list.Count; i++)
            {
                var item = list[i];
                int catId = 46;
                if (item.title.Contains("华为"))
                    catId = 18;

                if (item.title.Contains("衣"))
                    catId = 7;

                if (item.title.Contains("室"))
                    catId = 47;

                if (item.title.Contains("发"))
                    catId = 34;
                if (item.title.Contains("面膜"))
                    catId = 35;

                if (item.title.Contains("包"))
                    catId = 29;


                sql += $@" INSERT INTO `unionmallddd`.`tgoods`(`Id`, `TenantId`, `CategoryId`, `BrandId`, 
`ChainStoreId`, `Name`, `Status`, `Type`, `RecommendType`, `Image`, `Click`, `Stock`, `SellCount`, 
`RetailPrice`, `Price`, `Commission`, `Sort`, `Remark`, 
                `Detail`, `Parameter1`, `Parameter2`, `Parameter3`, `Parameter4`, `Parameter5`,
`Parameter6`, `Parameter7`, `Parameter8`, `Parameter9`, `Parameter10`) 
VALUES (default, 1, {catId}, 31, 17, '{item.title}', 2, 1, 'Rec',
'{item._id}', {randPrice.Next(1, 10000)}, {randPrice.Next(1, 10000)}, {randPrice.Next(1, 10000)}, {randPrice.NextDouble()}, {randPrice.Next(1, 10000)}, {randPrice.Next(1, 100)}, 0, 
'', '', '', '', '', '', '', '', '', '', '', '');
                ";

                if (i % 1000 == 0 || list.Count - i <= 1000)
                {
                   // db.Db.Ado.ExecuteCommandAsync(sql);
                   // Thread.Sleep(3000);
                    sql = string.Empty;
                }


            }

            Console.WriteLine("数据插入完毕!");
            Console.ReadLine();
        }



        public static string GetFileJson(string filepath)
        {
            string json = string.Empty;
            using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);//注册简体中文的支持
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
                {
                    json = sr.ReadToEnd().ToString();
                }
            }
            return json;
        }
    }




}

